package com.bjnangle.nangletool;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by YFB on 2017/8/17.
 */

public class TTSActivity extends BaseActivity implements View.OnClickListener{
    private TextView tvTTS;
    private TextToSpeech tts;
    private boolean isSpeak;
    private TextView tvTitle;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tts);
        setActivity(this);
        tvTTS = (TextView)findViewById(R.id.tv_tts);
        tts = new TextToSpeech(this, new MyOnInitialListener());
        String engine = tts.getDefaultEngine();
        if(engine!=null&&!engine.equals("")){
            if(engine.equals("com.google.android.tts")){
                tvTTS.setText("Google文字转语音引擎");
            }else if(engine.equals("com.iflytek.speechcloud")){
                tvTTS.setText("科大讯飞语音引擎");
                isSpeak = true;
            }else{
                tvTTS.setText(engine);
            }
        }
        ((Button)findViewById(R.id.btn_TTS_test)).setOnClickListener(this);

        ivGoBack = (ImageView)findViewById(R.id.btn_back);
        tvTitle = (TextView)findViewById(R.id.tv_title);
        tvTitle.setText("TTS设置");

    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_TTS_test){
            ttsTest();
        }
    }
    private void ttsTest(){
        if(isSpeak) {
            tts.speak("这是中文合成效果", TextToSpeech.QUEUE_FLUSH, null);
        }else{
            Toast.makeText(TTSActivity.this,"请在设置--语言和输入法中选择讯飞引擎",Toast.LENGTH_LONG).show();
        }
    }
    class MyOnInitialListener implements TextToSpeech.OnInitListener {

        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS){

            }

        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_1:
                ttsTest();
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
